package LeetCode.leetcode19;

public class Review3 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if (head == null) return null;
        ListNode virtualNode = new ListNode();
        virtualNode.next = head;
        ListNode l = virtualNode;
        ListNode r = head;
        for (int i = 0; i < n; i++) {
            if (r != null){
                r = r.next;
            }else
                return null;
        }
        while (r != null){
            l = l.next;
            r = r.next;
        }
        l.next = l.next.next;
        return virtualNode.next;
    }
}
